package com.promise.jokerdream.work.signin.midautumn

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 中秋礼盒工作
class MidAutumnGiftWork(uin: String): BaseWork(uin) {

    override val name: String
        get() = "中秋礼盒"

    override val priority: Int
        get() = 9

    override val isActWork: Boolean
        get() = true
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        return try {
            // 1. 先查询礼盒状态
            val queryResult = gameApi.queryMidAutumnGift()
            queryResult.fold(
                onSuccess = { response ->
                    // 2. 循环检查并领取可领取的礼盒
                    var currentResponse = response
                    var hasRewards = true
                    
                    while (hasRewards) {
                        hasRewards = false
                        
                        // 检查每个礼盒是否可以领取
                        for (gift in currentResponse.packageList) {
                            // 判断条件：finishNum >= limitNum && awardLevelNum < levelNum
                            if (gift.finishNum >= gift.limitNum && gift.awardLevelNum < gift.levelNum) {
                                // 可以领取，执行领取操作
                                val rewardResult = gameApi.getMidAutumnGift(gift.taskId)
                                rewardResult.fold(
                                    onSuccess = { rewardResponse ->
                                        historyManager.addHistory(name, rewardResponse.getDescription())
                                        currentResponse = rewardResponse
                                        hasRewards = true // 继续检查是否还有其他可领取的礼盒
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                    }
                                )
                                break // 领取一个后重新查询状态
                            }
                        }
                    }
                    
                    // 3. 检查并领取神秘奖励
                    if (currentResponse.mysteryPacket == 1) {
                        val mysteryResult = gameApi.getMidAutumnMysteryReward()
                        mysteryResult.fold(
                            onSuccess = { mysteryResponse ->
                                historyManager.addHistory(name, mysteryResponse.getDescription())
                            },
                            onFailure = { exception ->
                                historyManager.addHistory(name, exception.message)
                            }
                        )
                    }
                    
                    WorkResult.Success
                },
                onFailure = { exception ->
                    historyManager.addHistory(name, exception.message)
                    WorkResult.Failure(exception, exception.message)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
}